import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cart: [],
  },

  mutations: {
    changeCart(state, val) {
      if (state.cart.length === 0) {
        state.cart.push(val)
        val.checked = false
      } else {
        var flag = false
        state.cart.forEach(item => {
          if (val.basicInfo.id === item.basicInfo.id) {
            item.num += val.num
            flag = true
            return
          }
        })
        if(!flag){
          val.checked = false
          state.cart.push(val)
        }
      }
    },
  },
  actions: {},
  modules: {},
  plugins: [createPersistedState()],
})